package cn.flying.cloud.uid.api.feign;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.flying.cloud.base.common.exception.BizException;
import cn.flying.cloud.uid.api.UidServiceApi;
import cn.flying.cloud.uid.impl.CachedUidGenerator;
import cn.flying.cloud.uid.impl.DefaultUidGenerator;

/**
 * 全局ID服务接口
 *
 * @author: admin
 * @date: 2024年02月07日 16:08
 * @version: 1.0
 */
@RestController
@RequestMapping("/uid")
public class UidApiController implements UidServiceApi {

    @Resource
    private CachedUidGenerator cachedUidGenerator;
    @Resource
    private DefaultUidGenerator defaultUidGenerator;

    /**
     * 获取一个分布式全局ID
     *
     * @return
     */
    @Override
    public String getUid() {
        long uid = Math.abs(cachedUidGenerator.getUID());
        if (uid < 0) {
            throw new BizException("获取ID异常，为负值");
        }
        return String.valueOf(uid);
    }

    /**
     * 获取uid，默认本地雪花算法
     *
     * @return
     */
    @Override
    public String getLocalUid() {
        long uid = Math.abs(defaultUidGenerator.getUID());
        if (uid < 0) {
            throw new BizException("获取ID异常，为负值");
        }
        return String.valueOf(uid);
    }

    /**
     * 获取一个全局的单据编号
     *
     * @param prefix
     * @param length
     * @return
     */
    @Override
    public String getDocNo(String prefix, int length) {
        return null;
    }
}
